#include "datacollection.hpp"
#include <sstream>
#include <iostream>
using std::stringstream;
using namespace com_oocl_riley;
using std::pair;
using std::endl;

Node::Node(const string & dateTime, const size_t& size)
    :_dateTime(dateTime), _size(size)
{
}

bool Node::operator==(const Node& right)
{
    return _dateTime.compare(right._dateTime) == 0 ? _size == right._size : false;
}
bool Node::operator==(const Node& right) const
{
    return _dateTime.compare(right._dateTime) == 0 ? _size == right._size : false;
}
DataCollection::DataCollection()
    :_dataSet(nullptr)
{
    _dataSet = new map<KeyType, ValueType>();
}

DataCollection::~DataCollection()
{
    delete(_dataSet);
}

bool DataCollection::isEmpty() const
{
    return _dataSet->empty();
}

void DataCollection::insert(const KeyType & key, const ValueType & value)
{
    _dataSet->insert(pair<KeyType, ValueType>(key, value));
}

void DataCollection::erase(const KeyType & key)
{
    _dataSet->erase(key);
}

void DataCollection::clear()
{
    _dataSet->clear();
}

size_t DataCollection::size() const
{
    return _dataSet->size();
}

DataCollection * DataCollection::operator-(const DataCollection * right) const
{
    DataCollection *result = new DataCollection;
    if (this == right)
        return result;
    for (map<KeyType, ValueType>::const_iterator it = _dataSet->cbegin(); it != _dataSet->cend(); ++it)
    {
        if ((*right)[it->first] == it->second)
            continue;
        else
            result->insert(it->first, it->second);
    }
    return result;
}

ValueType & DataCollection::operator[](const KeyType &key)
{
    return _dataSet->operator[](key);
}

const ValueType & DataCollection::operator[](const KeyType& key) const
{
    return _dataSet->operator[](key);
}

string DataCollection::toString() const
{
    string result;
    stringstream ss;
    for (map<KeyType, ValueType>::const_iterator it = _dataSet->cbegin(); it != _dataSet->cend(); ++it)
    {
        string tmp;
        ss << it->first << " ";
        ss << it->second._dateTime << " ";
        ss << it->second._size;
        ss << endl;
        getline(ss, tmp);
        result += tmp + "\r\n";
    }
    return result;
}
